<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class ForgetpasswordApiController extends AbstractRestfulController
{
    public function getList()
    {			
			
    }
    public function get($token)
    {
		header('Access-Control-Allow-Origin: *');
		$forgetpasswordTable=$this->getServiceLocator()->get('Models\Model\ForgetpasswordFactory');
		$tokeninfo=array();
		$exitToke=0;
		$tokenExit=$forgetpasswordTable->gettoken($token);		
		if(count($tokenExit)!=0){		
			foreach($tokenExit as $tokeninfo){}				
				if($tokeninfo->forget_pwd_id > 0){
					$exitToken=1;
				}else{
					$exitToken=0;
				}
		}else{
			$exitToken=0;
		}
		$result = new JsonModel(array(
			'existtoken'=>$exitToken,
		));	
		return $result;	
    }
    public function create($s_eamil)
    {
		header('Access-Control-Allow-Origin: *');
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$forgetpasswordTable=$this->getServiceLocator()->get('Models\Model\ForgetpasswordFactory');
		global $forgotPasswordSubject,$frogotPasswordMessage;
		$mailinfo=array();
		if(!empty($s_eamil['email'])){		
			$email=$s_eamil['email'];			
			$uniqueRecardemail = $userTable->checkUniqueRecord($email);			
			if($uniqueRecardemail>0){				
				$userinfo=$userTable->getMembersByMailId($email)->current();	
				$user_id=$userinfo->user_id;
				$username=ucfirst($userinfo->user_name);				
				$token = getUniqueCode('10');				
				$tokentime = date("Y-m-d H:i:s", time()) ;				
				$mailinfo=$forgetpasswordTable->getmailfromfgtpwd($email)->current();		
				try {	
					if(count($mailinfo)!=""){					
						$alreadyexitid=$mailinfo->forget_pwd_id;
						$insForgotPassword = $forgetpasswordTable->addForgetpwd($alreadyexitid,$user_id,$email,$token);
					}else{						
						$alreadyexitid="";
						$insForgotPassword = $forgetpasswordTable->addForgetpwd($alreadyexitid,$user_id,$email,$token);	
					}
				}catch (\Exception $ex) {
					$insForgotPassword="";
				}				
				$frogotPasswordMessage = str_replace("<FULLNAME>","$username", $frogotPasswordMessage);
				$frogotPasswordMessage = str_replace("<PASSWORDLINK>","http://localhost/rest/newpassword.php?token=".$token, $frogotPasswordMessage);	
				$to=$email;								
				if(sendMail($to,$forgotPasswordSubject,$frogotPasswordMessage)){
					$result = new JsonModel(array(					
						'output' => 'success',					
					));	
				}else{
					$result = new JsonModel(array(					
						'output' => 'fail',
					));	
				}
				
			}else{
				$result = new JsonModel(array(					
					'output' => 'fail',
				));
			}
		}else{
			$result = new JsonModel(array(					
				'output' => 'fail',
			));
		}
		return $result;
    }
    public function update($token, $pwd)
    {
		header('Access-Control-Allow-Origin: *');
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$forgetpasswordTable=$this->getServiceLocator()->get('Models\Model\ForgetpasswordFactory');
		$tokenExit=$forgetpasswordTable->gettoken($token);
		$token=$tokenExit->toarray();
		if(count($token)!=0){					
			$changepwd = $userTable->changepwd($token[0]['user_id'],$pwd['cnfpwrd']);			
			if($changepwd>=0){
				$userAuth=$userTable->updateUserRegAuth($token[0]['user_id']);
				$users=$userTable->userdetails($token[0]['user_id']);			
				$deletetokenid=$forgetpasswordTable->deletetoken($token[0]['forget_pwd_id']);			
				$result = new JsonModel(array(					
					'output' => 'success',
				));			
			}else{
				$result = new JsonModel(array(					
					'output' => 'fail',
				));
			}
			return $result;	
		}else{
			return new JsonModel(array(					
				'output' => 'fail',
			));
		}
    }
    public function delete($id)
    {
       
    }
}